<?php
    include_once("../action/checkAuthenticationAction.php");
    include_once("../service/articleService.php");
    include_once("../service/bannerService.php");
    include_once("../util/constant.php");
    include_once("../util/message.php");
    
    include_once("../util/authUtil.php");
    $perm_array = $_SESSION["permissions"];
?>
<!DOCTYPE html>
<html lang="en">
<head>
	<title>Chi tiết banner</title>
	<?php include("import-css.php");?>
    <?php include("import-css-upload.php");?>    
</head>
<body>
	<?php include("header.php");?>
    
    <div class="container-fluid">
		<div class="row-fluid">
            <!--Menu-->
            <?php include("menu.php");?>
            <?php
                $action = isset($_GET["action"]) ? $_GET["action"] : NEW_ACTION;
                $_SESSION["newAction"] = "1";
                unset($_SESSION["banner_id"]);
                $_SESSION["uploadMode"] = UPLOAD_MODE_BANNER;
                                                             
                if($action == EDIT_ACTION){
                    $banner_id = $_GET["id"];
                    $banner = getBanner($banner_id);
                    $_SESSION["banner_id"] = $banner["BANNER_ID"];
                    unset($_SESSION["newAction"]);                                        
                }
            ?>
            <!--Content-->
            <div class="box span10 offset2">
                <div class="box-content">
                <?php
                if(!authUtil(array("edit banner","create banner","view banner"),$perm_array,false)){
                    echo "<script>window.location.href='../admin/index.php';</script>";
                    
                    $require_perm = array("edit banner","create banner");
                }
                ?>
                    <form class="form-horizontal" method="POST" action="<?php if(authUtil($require_perm,$perm_array,false)){echo (($action == NEW_ACTION) ? "../action/newBannerAction.php" : "../action/editBannerAction.php");} ?>" id="fileupload" enctype="multipart/form-data">
                        <div class="control-group">
                            <label class="control-label" for="selectError">Thuộc nhóm</label>
                            <div class="controls">
                                 <select required id="type" name="type">
                                    <option value=''>----------</option>
                                    <?php
                                        for($i=1;$i<=4;$i++){
                                    ?>
                                        
                                        <option 
                                        <?php
                                            if(isset($banner["BANNER_ID"])){
                                                echo ($banner["BANNER_TYPE"]==$i)?'selected':'';
                                            }
                                        ?> 
                                            value='<?php echo $i; ?>'>
                                            <?php if($i==1) echo "Banner trái"; else if($i==2) echo "Banner giữa"; else if($i==3) echo "Banner phải"; else echo "Banner sự kiện"; ?>                                        
                                        </option>                                       
                                    <?php        
                                        }
                                    ?>
                                 </select>
                            </div>
                        </div>
                        <div class="control-group">
                            <label class="control-label" for="selectError">Bài viết, Sự kiện</label>
                            <div class="controls">
                                 <select required id="event" name="event">
                                    <?php
                                        $events = getAllArticle();  
                                    ?>
                                    <option value=''>----------</option>
                                    <?php
                                        while($event = mysql_fetch_array($events)){
                                    ?>
                                        <option 
                                        <?php
                                            if(isset($banner_id)){
                                                echo $event["ART_ID"]==$banner["BANNER_EVE_ID"]?'selected':'';
                                            }
                                        ?> 
                                            value='<?php echo $event["ART_ID"]; ?>'>
                                            <?php echo $event["ART_TITLE"]; ?>                                        
                                        </option>
                                    <?php        
                                        }
                                    ?>
                                 </select>
                            </div>
                        </div>
                        <div class="control-group">
                            <label for="thumbnail" class="control-label">Hình đại diện</label>
                            <div class="controls">
                                <input type="text" value="<?php echo ($action == EDIT_ACTION ?  $banner["BANNER_IMG_PATH"] : ""); ?>" id="thumbnail" name="thumbnail" class="input-xlarge focused">
                                <?php
                                    if($action == EDIT_ACTION && strlen($banner["BANNER_IMG_PATH"])>0){  
                                ?>
                                    <a target="_blank" href="<?php echo $banner["BANNER_IMG_PATH"]; ?>">Xem hình</a>
                                <?php
                                    }
                                ?>
                                <div>Kích thước (rộng x cao): 978 x 90 [banner trai va phai]</div>
                                <div>Kích thước (rộng x cao): 241 x 496 [banner su kien]</div>
                            </div>
                        </div>
                        <?php
                            if(isset($banner["BANNER_ID"])){
                        ?>
                            <?php
                                if(authUtil(array("edit banner","create banner"),$perm_array,false)){
                            ?>  
                            <div class="control-group">
                                <label class="control-label" for="textarea2">Hình ảnh</label>
                                <div class="controls">
                                    <div class="row fileupload-buttonbar" style="margin: 3px;">
                                        <div class="col-lg-7">
                                            <!-- The fileinput-button span is used to style the file input field as button -->
                                            <span class="btn btn-success fileinput-button">
                                                <i class="glyphicon glyphicon-plus"></i>
                                                <span>Thêm hình</span>
                                                <input type="file" name="files[]" multiple>
                                            </span>
                                            <button type="submit" class="btn btn-primary start">
                                                <i class="glyphicon glyphicon-upload"></i>
                                                <span>Tải hình</span>
                                            </button>
                                            <button type="button" class="btn btn-danger delete" onclick="">
                                                <i class="glyphicon glyphicon-trash"></i>
                                                <span>Xóa</span>
                                            </button>
                                            <input type="checkbox" class="toggle">
                                            <!-- The loading indicator is shown during file processing -->
                                            <span class="fileupload-loading"></span>
                                        </div>
                                        <!-- The global progress information -->
                                        <div class="col-lg-5 fileupload-progress fade">
                                            <!-- The global progress bar -->
                                            <div class="progress progress-striped active" role="progressbar" aria-valuemin="0" aria-valuemax="100">
                                                <div class="progress-bar progress-bar-success" style="width:0%;"></div>
                                            </div>
                                            <!-- The extended global progress information -->
                                            <div class="progress-extended">&nbsp;</div>
                                        </div>
                                    </div>
                                </div>
                            </div>  
                            <table role="presentation" class="table table-striped"><tbody class="files"></tbody></table>
                            <?php
                                }
                            ?>
                        <?php
                        }
                        ?>  
                        <?php
                            if(authUtil($require_perm,$perm_array,false)){
                        ?>                      
                            <center>
                            <button type="submit" class="btn btn-primary" ><?php echo (($action == NEW_ACTION) ? "Tạo mới" : "Cập nhật"); ?></button>
                            </center>
                        <?php
                            }
                        ?>
                        <input type="hidden" name="edit_banner_id" id="edit_banner_id" value="<?php echo isset($banner["BANNER_ID"])?$banner["BANNER_ID"]:"0"; ?>"/>
                        <input type="hidden" name="imgPaths" id="imgPaths" />                        
                    </form>
                </div>
                
            </div><!--/span-->
        </div>
    </div>

    <?php include("footer.php");?>
	<!-- The template to display files available for upload -->
		<script id="template-upload" type="text/x-tmpl">
        {% for (var i=0, file; file=o.files[i]; i++) { %}
            <tr class="template-upload fade">
                <td>
                    <span class="preview"></span>
                </td>
                <td>
                    <p class="name">{%=file.name%}</p>
                    {% if (file.error) { %}
                        <div><span class="label label-danger">Lỗi</span> {%=file.error%}</div>
                    {% } %}
                </td>
                <td>
                    <p class="size">{%=o.formatFileSize(file.size)%}</p>
                    {% if (!o.files.error) { %}
                        <div class="progress progress-striped active" role="progressbar" aria-valuemin="0" aria-valuemax="100" aria-valuenow="0"><div class="progress-bar progress-bar-success" style="width:0%;"></div></div>
                    {% } %}
                </td>
                
                <td>
                    {% if (!o.files.error && !i && !o.options.autoUpload) { %}
                        <button class="btn btn-primary start" style="visibility: hidden;">
                            <i class="glyphicon glyphicon-upload"></i>
                            <span>Bắt đầu</span>
                        </button>
                    {% } %}
                    {% if (!i) { %}
                        <button class="btn btn-warning cancel">
                            <i class="glyphicon glyphicon-ban-circle"></i>
                            <span>Hủy</span>
                        </button>
                    {% } %}
                </td>
            </tr>
        {% } %}
        </script>
        <!-- The template to display files available for download -->
        <script id="template-download" type="text/x-tmpl">
        {% for (var i=0, file; file=o.files[i]; i++) { %}
            <tr class="template-download fade">
                <td>
                    <span class="preview">
                        {% if (file.url) { %}
                            <a href="{%=file.url%}" title="{%=file.name%}" download="{%=file.name%}" data-gallery><img src="{%=file.url%}"></a>
                        {% } %}
                    </span>
                </td>
                <td>
                    <p class="name">
                        {% if (file.url) { %}
                            <a href="{%=file.url%}" title="{%=file.name%}" download="{%=file.name%}" {%=file.url?'data-gallery':''%}>{%=file.name%}</a>
                        {% } else { %}
                            <span>{%=file.name%}</span>
                        {% } %}
                    </p>
                    {% if (file.error) { %}
                        <div><span class="label label-danger">Lỗi</span> {%=file.error%}</div>
                    {% } %}
                </td>
                <td>
                    <span class="size">{%=o.formatFileSize(file.size)%}</span>
                </td>
                <td>
                    <p class="name">
                        {% if (file.url) { %}
                                <span onclick="$(this).selectText();">{%=file.url%}</span>
                        {% } %}
                    </p>
                </td>
                <td>
                    {% if (file.deleteUrl) { %}
                        <button class="btn btn-danger delete" data-type="{%=file.deleteType%}" data-url="{%=file.deleteUrl%}"{% if (file.deleteWithCredentials) { %} data-xhr-fields='{"withCredentials":true}'{% } %}>
                            <i class="glyphicon glyphicon-trash"></i>
                            <span>Xóa</span>
                        </button>
                        <input type="checkbox" name="delete" value="{%=file.name%}" class="toggle">
                    {% } else { %}
                        <button class="btn btn-warning cancel">
                            <i class="glyphicon glyphicon-ban-circle"></i>
                            <span>Hủy</span>
                        </button>
                    {% } %}
                </td>
            </tr>
        {% } %}
        </script>	
	</div><!--/.fluid-container-->

	<?php include("import-js.php");?>
    <?php include("import-js-upload.php");?>	
    <script>    
    // Initialize the jQuery File Upload widget:
		$('#fileupload').fileupload({
			url: '../image/',
			maxFileSize: <?php echo MAX_FILE_SIZE;?>,
			acceptFileTypes: /(\.|\/)(gif|jpe?g|png)$/i
		}).bind('fileuploadalways', function (e, data) {
			
		});
    </script>    
</body>
</html>

